package com.party.notify.notifyPush.officialPartner.impl;

import com.google.common.collect.Maps;
import com.party.common.utils.BigDecimalUtils;
import com.party.core.model.activity.Activity;
import com.party.core.model.copartner.BrokerageItem;
import com.party.core.model.crowdfund.Project;
import com.party.core.model.crowdfund.TargetProject;
import com.party.core.model.member.Member;
import com.party.core.model.message.IdentityType;
import com.party.core.model.message.MessageTag;
import com.party.core.model.order.OrderForm;
import com.party.core.model.wallet.Withdrawals;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.crowdfund.ITargetProjectService;
import com.party.notify.notifyPush.officialPartner.ICopartnerNotifySendService;
import com.party.notify.notifyPush.servce.INotifyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @Author: Administrator.
 * @Description: 合伙人APP消息推送
 * @Date:Created in 2017/10/30 0030 下午 16:46.
 */
@Service
public class CopartnerNotifySendService implements ICopartnerNotifySendService {
    @Autowired
    private INotifyService notifyService;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private ITargetProjectService targetProjectService;

    @Value("#{jpush['jpush.copartner.appKey']}")
    private String appKey;

    @Value("#{jpush['jpush.copartner.masterSecret']}")
    private String masterSecret;

    /**
     * 发起众筹
     */
    private static final String COPARTNER_CROWDFUND_APPLY = "copartnerCrowdfundApply";

    /**
     * 发起众筹__多场
     */
    private static final String MULTIPLE_COPARTNER_CROWDFUND_APPLY = "multipleCopartnerCrowdfundApply";

    /**
     * 众筹成功__多场
     */
    private static final String MULTIPLE_COPARTNER_CROWDFUND_SUCCESS = "multipleCopartnerCrowdfundSuccess";

    /**
     * 众筹成功
     */
    private static final String COPARTNER_CROWDFUND_SUCCESS = "copartnerCrowdfundSuccess";
    /**
     * 分配项目
     */
    private static final String COPARTNER_ALLOT_ZC_ACTIVITY = "copartnerAllotZcActivity";
    /**
     * 佣金第一次审核通过
     */
    private static final String BROKERAGE_FIRST_VERIFY_PASS = "brokerageFirstVerifyPass";
    /**
     * 佣金第二次审核通过
     */
    private static final String BROKERAGE_SECOND_VERIFY_PASS = "brokerageSecondVerifyPass";
    /**
     * 佣金第二次审核拒绝
     */
    private static final String BROKERAGE_SECOND_VERIFY_FAIL = "brokerageSecondVerifyFail";
    /**
     * 佣金提现通过推送
     */
    private static final String BROKERAGE_WITHDRAWAL_PASS = "brokerageWithdrawalPass";
    /**
     * 佣金提现拒绝推送
     */
    private static final String BROKERAGE_WITHDRAWAL_FAIL = "brokerageWithdrawalFail";

    /**
     * 新的合伙人加入推送
     */
    private static final String REGISTER_SUCCESS = "copartnerRegisterSuccess";

    /**
     * 直接佣金推送 众筹
     */
    private static final String DIRECT_BROKERAGE_ZC = "copartnerDirectBrokerage";

    /**
     * 间接佣金推送 众筹
     */
    private static final String INDIRECT_BROKERAGE_ZC = "copartnerIndirectBrokerage";

    /**
     * 直接佣金推送 课程
     */
    private static final String DIRECT_BROKERAGE_KC = "copartnerDirectBrokerageCourse";

    /**
     * 间接佣金推送 课程
     */
    private static final String INDIRECT_BROKERAGE_KC = "copartnerIndirectBrokerageCourse";



    protected static Logger logger = LoggerFactory.getLogger(CopartnerNotifySendService.class);
    /**
     * 发起众筹通知
     *
     * @param project  项目
     * @param member   会员
     * @param receiverId 接受者id
     * @param receiverMobile 接受者mobile
     */
    @Override
    public void sendCrowdfundApply(Project project, Member member, String receiverId, String receiverMobile, String identityType) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", member.getRealname());
        constant.put("{project}", project.getTitle());
        constant.put("relId", project.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", receiverId);
        constant.put("identityType", identityType);
        notifyService.push(COPARTNER_CROWDFUND_APPLY, receiverMobile,
                receiverId, constant, null, appKey, masterSecret);
    }

    /**
     * 发起众筹通知
     *
     * @param project  项目
     * @param member   会员
     * @param receiverId 接受者id
     * @param receiverMobile 接受者mobile
     */
    @Override
    public void gradationSendCrowdfundApply(Project project, Member member, String receiverId, String receiverMobile, String identityType) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", member.getRealname());
        constant.put("{project}", project.getTitle());
        constant.put("{gradationName}", project.getGradationName());
        constant.put("relId", project.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", receiverId);
        constant.put("identityType", identityType);

        logger.info("发起众筹通知____"+MULTIPLE_COPARTNER_CROWDFUND_APPLY+"--------"+receiverMobile+"--------"+
                receiverId+"--------"+ constant+"--------"+null+"--------"+appKey+"--------"+masterSecret);

        notifyService.push(MULTIPLE_COPARTNER_CROWDFUND_APPLY, receiverMobile,
                receiverId, constant, null, appKey, masterSecret);
    }

    /**
     * 众筹成功通知
     *
     * @param orderForm       订单
     * @param crowdfundMember 众筹者
     * @param disMemberId       分销者id
     * @param disMemberMobile   分销者mobile
     * @param projectId       众筹id
     * @param identityType    身份类型
     */
    @Override
    public void sendCrowdfundSuccess(OrderForm orderForm, Member crowdfundMember, String disMemberId, String disMemberMobile, String identityType, String projectId) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", crowdfundMember.getRealname());
        constant.put("{project}", orderForm.getTitle());
        constant.put("relId", projectId);
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", disMemberId);
        constant.put("identityType", identityType);
        notifyService.push(COPARTNER_CROWDFUND_SUCCESS, disMemberMobile, disMemberId,
                constant, null, appKey, masterSecret);
    }

    /**
     * 分配项目
     *
     * @param activity 项目
     * @param receiver 接受者
     */
    @Override
    public void sendAllotZcActivity(Activity activity, Member receiver) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{project}", activity.getTitle());
        constant.put("relId", activity.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", receiver.getId());
        constant.put("identityType", IdentityType.PARTNER.getCode());
        notifyService.push(COPARTNER_ALLOT_ZC_ACTIVITY, receiver.getMobile(), receiver.getId(),
                constant, null, appKey, masterSecret);
    }

    /**
     * 佣金第一次审核通过
     *
     * @param realname 众筹者名称
     * @param title    项目标题
     * @param item     佣金流水
     * @param receiver 接受者
     */
    @Override
    public void sendBrokerageFirstVerifyPass(String realname, String title, BrokerageItem item, Member receiver) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", realname);
        constant.put("{project}", title);
        constant.put("{brokerage}", item.getBrokerage());
        constant.put("relId", item.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", receiver.getId());
        constant.put("identityType", IdentityType.PARTNER.getCode());
        notifyService.push(BROKERAGE_FIRST_VERIFY_PASS, receiver.getMobile(), receiver.getId(),
                constant, null, appKey, masterSecret);
    }

    /**
     * 佣金第二次审核通过
     *
     * @param realname 众筹者名称
     * @param title    项目标题
     * @param item     佣金流水
     * @param receiver 接受者
     */
    @Override
    public void sendBrokerageSecondVerifyPass(String realname, String title, BrokerageItem item, Member receiver) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", realname);
        constant.put("{project}", title);
        constant.put("{brokerage}", item.getBrokerage());
        constant.put("relId", item.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", receiver.getId());
        constant.put("identityType", IdentityType.PARTNER.getCode());
        notifyService.push(BROKERAGE_SECOND_VERIFY_PASS, receiver.getMobile(), receiver.getId(),
                constant, null, appKey, masterSecret);
    }

    /**
     * 佣金第二次审核拒绝
     *
     * @param realname 众筹者名称
     * @param title    项目标题
     * @param item     佣金流水
     * @param receiver 接受者
     */
    @Override
    public void sendBrokerageSecondVerifyFail(String realname, String title, BrokerageItem item, Member receiver) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{user}", realname);
        constant.put("{project}", title);
        constant.put("{brokerage}", item.getBrokerage());
        constant.put("relId", item.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", receiver.getId());
        constant.put("identityType", IdentityType.PARTNER.getCode());
        notifyService.push(BROKERAGE_SECOND_VERIFY_FAIL, receiver.getMobile(), receiver.getId(),
                constant, null, appKey, masterSecret);
    }

    /**
     * 佣金提现审核通过
     *
     * @param withdrawals 提现
     * @param receiver    接受者
     */
    @Override
    public void sendBrokerageWithdrawalPass(Withdrawals withdrawals, Member receiver) {
        // 手续费
        double serviceFee = BigDecimalUtils.mul(withdrawals.getPayment(), Withdrawals.SERVER_FEE);
        serviceFee = BigDecimalUtils.round(serviceFee, 2);

        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{brokerage}", withdrawals.getPayment());
        constant.put("{serverFee}", serviceFee);
        constant.put("relId", withdrawals.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", receiver.getId());
        constant.put("identityType", IdentityType.CHANNEL_DIS.getCode());
        notifyService.push(BROKERAGE_WITHDRAWAL_PASS, receiver.getMobile(), receiver.getId(),
                constant, null, appKey, masterSecret);
    }

    /**
     * 佣金提现审核拒绝
     *
     * @param withdrawals 提现
     * @param receiver    接受者
     */
    @Override
    public void sendBrokerageWithdrawalFail(Withdrawals withdrawals, Member receiver) {
        // 手续费
        double serviceFee = BigDecimalUtils.mul(withdrawals.getPayment(), Withdrawals.SERVER_FEE);
        serviceFee = BigDecimalUtils.round(serviceFee, 2);

        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{brokerage}", withdrawals.getPayment());
        constant.put("{serverFee}", serviceFee);
        constant.put("relId", withdrawals.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", receiver.getId());
        constant.put("identityType", IdentityType.CHANNEL_DIS.getCode());
        notifyService.push(BROKERAGE_WITHDRAWAL_FAIL, receiver.getMobile(), receiver.getId(),
                constant, null, appKey, masterSecret);
    }

    @Override
    public void sendRegisterSuccess(String receiver, String realname) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{realname}", realname);
        constant.put("relId", "");
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", receiver);
        constant.put("identityType", IdentityType.CHANNEL_DIS.getCode());
        notifyService.push(REGISTER_SUCCESS, null, receiver,
                constant, null, appKey, masterSecret);
    }

    @Override
    public void sendDirectBrokerage(String receiver, String title, Double brokerage, String activityId, String realname, Integer brokerageType) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", title);
        constant.put("{brokerage}", brokerage);
        constant.put("{realname}", realname);
        constant.put("relId", activityId);
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", receiver);
        constant.put("identityType", IdentityType.CHANNEL_DIS.getCode());
        if (BrokerageItem.TYPE_CROWDFUND.equals(brokerageType)) {
            notifyService.push(DIRECT_BROKERAGE_ZC, null, receiver,
                    constant, null, appKey, masterSecret);
        } else if (BrokerageItem.TYPE_COURSE.equals(brokerageType)) {
            notifyService.push(DIRECT_BROKERAGE_KC, null, receiver,
                    constant, null, appKey, masterSecret);
        }
    }

    @Override
    public void sendIndirectBrokerage(String receiver, String title, Double brokerage, String realname, Integer brokerageType) {
        HashMap<String, Object> constant = Maps.newHashMap();
        constant.put("{title}", title);
        constant.put("{brokerage}", brokerage);
        constant.put("{realname}", realname);
        constant.put("relId", "");
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", receiver);
        constant.put("identityType", IdentityType.CHANNEL_DIS.getCode());
        if (BrokerageItem.TYPE_CROWDFUND.equals(brokerageType)) {
            notifyService.push(INDIRECT_BROKERAGE_ZC, null, receiver,
                    constant, null, appKey, masterSecret);
        } else if (BrokerageItem.TYPE_COURSE.equals(brokerageType)) {
            notifyService.push(INDIRECT_BROKERAGE_KC, null, receiver,
                    constant, null, appKey, masterSecret);
        }
    }

    /**
     * 众筹成功通知___多场
     *
     * @param project 订单
     * @param member    会员
     * @param receiver  接受者
     */
    @Override
    public void gradationSendCrowdfundSuccess(Project project, Member member, String receiverId, String receiverMobile, String identityType) {
        HashMap<String, Object> constant = Maps.newHashMap();

        constant.put("{user}", member.getRealname());
        constant.put("{project}", project.getTitle());
        constant.put("{gradationName}", project.getGradationName());
        constant.put("relId", project.getId());
        constant.put("tag", MessageTag.UNDEFIND.getCode());
        constant.put("createBy", receiverId);
        constant.put("identityType", identityType);

        logger.info("众筹成功通知___多场"+COPARTNER_CROWDFUND_SUCCESS+"________"+receiverMobile+"________"+receiverId+"________"+
                constant+"________"+ null+"________"+appKey+"________"+masterSecret);


        notifyService.push(COPARTNER_CROWDFUND_SUCCESS, receiverMobile, receiverId,
                constant, null, appKey, masterSecret);
    }
}
